package d2_com.itheima.io;

import java.io.*;

/**
 * 字符转换流
 */
public class Test6 {
    public static void main(String[] args) {
        /**
         * 字符输入转换流
         */
        try (
                //1、得到文件的原始字节流(6BK的字节流形式)
                InputStream fo = new FileInputStream("oop-app6\\8.txt");
                // 2、把原始的字节输入流按照指定的字符集编码转换成字符输入流
                BufferedReader gbk = new BufferedReader(new InputStreamReader(fo, "GBK"));
        ) {
            char[] buffer = new char[1024];
            int len;
            while ((len = gbk.read(buffer)) != -1) {
                System.out.println(new String(buffer, 0, len));
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        /**
         * 字符输出转换流
         */
        try (
                //1、得到文件的原始字节流(6BK的字节流形式)
                OutputStream fo = new FileOutputStream("oop-app6\\8.txt");
                // 2、把原始的字节输入流按照指定的字符集编码转换成字符输入流
                BufferedWriter gbk = new BufferedWriter(new OutputStreamWriter(fo, "GBK"));
        ) {
            gbk.write("离谱哦~");
            gbk.newLine();
            gbk.write("我是你爹");
        } catch (
                Exception e) {
            throw new RuntimeException(e);
        }
    }
}
